package com.hxzy.config.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hxzy.entity.SysUser;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/8/29 9:21
 * @Version 1.0
 */
@Getter
@Setter
@ToString
public class AdminLoginResp implements UserDetails {

    //当前用户信息
    private SysUser  sysUser;

    //角色权限
    private Set<String> rolePermissions;

    //菜单权限
    private Set<String> menuPermission;

    private String uuid;

    private Date expired;


    /**
     * spring security的权限,让redis序列化的时候，忽略它，直接走自己的验证逻辑，不走标准操作
     * @return
     */
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority>  set=new HashSet<>();
        //角色权限，必须要加一个"ROLE_"
        rolePermissions.stream().forEach(p-> {
            SimpleGrantedAuthority  s=new SimpleGrantedAuthority("ROLE_"+p);
            set.add(s);
        });

        //菜单权限
        menuPermission.stream().forEach(p-> {
            SimpleGrantedAuthority  s=new SimpleGrantedAuthority(p);
            set.add(s);
        });
        return set;
    }

    /**
     * 获取认证用户密码
     * @return
     */
    @JsonIgnore
    @Override
    public String getPassword() {
        return this.sysUser.getPassword();
    }

    /**
     * 获取登录名
     * @return
     */
    @JsonIgnore
    @Override
    public String getUsername() {
        return this.sysUser.getUserName();
    }

    /**
     * 账户是否未过期
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否未锁定
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 凭证是否未过期
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否被启用
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
